/*******************************************************************************

Copyright (C) 2023 - 2024 Qualcomm Technologies International, Ltd.
All Rights Reserved.
Qualcomm Technologies International, Ltd. Confidential and Proprietary.

*******************************************************************************/

#include "dmlib_private.h"

/*----------------------------------------------------------------------------*
 *  NAME
 *      dm_sm_encrypt_data_req
 *
 *  DESCRIPTION
 *      Build and send a DM_SM_ENCRYPT_DATA_REQ primitive.
 *      If pp_prim != NULL, primitive is returned, not sent.
 *
 *  RETURNS
 *      void
 *
 *----------------------------------------------------------------------------*/

void dm_sm_encrypt_data_req(phandle_t   phandle,
                            uint16_t    flags,
                            uint8_t     *session_key,
                            uint8_t     *iv,
                            uint8_t     *randomizer,
                            uint8_t     *mic,
                            uint16_t    data_len,
                            uint8_t     **data,
                            DM_UPRIM_T  **pp_prim)
{
    DM_SM_ENCRYPT_DATA_REQ_T *p_prim = zpnew(DM_SM_ENCRYPT_DATA_REQ_T);
    uint8_t i;
    uint16_t len = 0;

    p_prim->type = DM_SM_ENCRYPT_DATA_REQ;
    p_prim->phandle = phandle;
    p_prim->flags = flags;

    qbl_memscpy(p_prim->session_key,DM_SM_ENCRYPT_KEY_LEN,
                session_key, DM_SM_ENCRYPT_KEY_LEN);

    qbl_memscpy(p_prim->iv, DM_SM_ENCRYPT_IV_LEN, iv, DM_SM_ENCRYPT_IV_LEN);

    qbl_memscpy(p_prim->randomizer, DM_SM_ENCRYPT_RAND_LEN,
                randomizer, DM_SM_ENCRYPT_RAND_LEN);

    qbl_memscpy(p_prim->mic, DM_SM_ENCRYPT_MIC_LEN, mic, DM_SM_ENCRYPT_MIC_LEN);

    p_prim->data_len = data_len;
    for (i=0; (i < DM_SM_ENCRYPT_DATA_PTRS) && (data_len > len); i++)
    {
        p_prim->data[i] = data[i];
        data[i] = NULL;
        len += DM_SM_ENCRYPT_DATA_PER_PTR;
    }

    if (pp_prim)
    {
        *pp_prim = (DM_UPRIM_T *) p_prim;
    }
    else
    {
        DM_PutMsg(p_prim);
    }
}

